﻿using System;
using System.Collections.Generic;
using System.Text;
using General.Entities;
using System.Linq;
using General.Core.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using General.Core.Menu;
using System.IO;

namespace General.Services.Category
{
    public class CategoryService : ICategoryService
    {
        private static readonly string MODEL_KEY = "General.services.category";

        private IMemoryCache _memoryCache;
        private GeneralDbContext _generalDbContext;

        public CategoryService(IMemoryCache memoryCache,
            GeneralDbContext generalDbContext)
        {
            this._generalDbContext = generalDbContext;
            this._memoryCache = memoryCache;
        }

        /// <summary>
        /// 初始化保存方法
        /// </summary>
        /// <param name="list"></param>
        public void InitCategory()
        {
            var xmlSiteMap = new XmlSiteMap();
            xmlSiteMap.LoadFrom(Path.Combine(Directory.GetCurrentDirectory(), "sitemap.xml"));
            List<Entities.Category> list = new List<Entities.Category>();
            xmlSiteMap.SiteMapNodes.ForEach(item =>
            {
                list.Add(new Entities.Category()
                {
                    Action = item.Action ?? "",
                    Controller = item.Controller ?? "",
                    CssClass = item.IconClass ?? "",
                    IsMenu = item.IsMenu,
                    Name = item.Name,
                    RouteName = item.RouteName,
                    SysResource = item.SysResource,
                    Sort = item.Sort,
                    FatherID = item.FatherID ?? "",
                    ResouceID = item.ResouceID

                });
            });

            var oldList = _generalDbContext.Categories.ToList();
            oldList.ForEach(del =>
            {
                var item = list.FirstOrDefault(o => o.SysResource == del.SysResource);
                if (item == null)
                {
                    var permissionList = _generalDbContext.SysPermissions.Where(o => o.CategoryId == del.Id).ToList();
                    _generalDbContext.SysPermissions.RemoveRange(permissionList);
                    _generalDbContext.Categories.Remove(del);
                }
            });
            list.ForEach(entity =>
            {
                var item = oldList.FirstOrDefault(o => o.SysResource == entity.SysResource);
                if (item == null)
                {
                    _generalDbContext.Categories.Add(entity);
                }
                else
                {
                    item.Action = entity.Action;
                    item.Controller = entity.Controller;
                    item.CssClass = entity.CssClass;
                    item.IsMenu = entity.IsMenu;
                    item.Name = entity.Name;
                    item.RouteName = entity.RouteName;
                    item.SysResource = entity.SysResource;
                    item.Sort = entity.Sort;
                    item.FatherID = entity.FatherID;
                    item.ResouceID = entity.ResouceID;
                }
            });
            _generalDbContext.SaveChanges();
            _memoryCache.Remove(MODEL_KEY);
        }

        /// <summary>
        /// 获取所有的菜单数据 并缓存
        /// </summary>
        /// <returns></returns>
        public List<Entities.Category> GetAllCache()
        {
            List<Entities.Category> list = null;
            if (!_memoryCache.TryGetValue<List<Entities.Category>>(MODEL_KEY, out list))
            {
                list = _generalDbContext.Categories.ToList();
                _memoryCache.Set(MODEL_KEY, list, DateTimeOffset.Now.AddDays(1));
            }
            return list;
        }
    }
}
